﻿using System.Windows;
using System.Windows.Controls;

namespace UI.Controls
{
    /// <summary>
    /// Interaction logic for OkCancel.xaml
    /// </summary>
    public partial class OkCancel : UserControl
    {
        public OkCancel()
        {
            InitializeComponent();
        }

        public event RoutedEventHandler OkClicked
        {
            add { AddHandler(OkClickedEvent, value);}
            remove {RemoveHandler(OkClickedEvent, value);}
        }
        public event RoutedEventHandler CancelClicked
        {
            add { AddHandler(CancelClickedEvent, value); }
            remove { RemoveHandler(CancelClickedEvent, value); }
        }

        public static readonly RoutedEvent OkClickedEvent = EventManager.RegisterRoutedEvent("OkClicked",
                                                                                             RoutingStrategy.Bubble,
                                                                                             typeof (RoutedEventHandler),
                                                                                             typeof(OkCancel));
        public static readonly RoutedEvent CancelClickedEvent = EventManager.RegisterRoutedEvent("CancelClicked",
                                                                                             RoutingStrategy.Bubble,
                                                                                             typeof(RoutedEventHandler),
                                                                                             typeof(OkCancel));

        void RaiseOkClickedEvent(object source)
        {
            RoutedEventArgs args = new RoutedEventArgs(OkClickedEvent, source);
            RaiseEvent(args);
        }
        
        void RaiseCancelClickedEvent(object source)
        {
            RoutedEventArgs args = new RoutedEventArgs(CancelClickedEvent, source);
            RaiseEvent(args);
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            FrameworkElement element = e.Source as FrameworkElement;
            if (element != null)
                switch (element.Name)
                {
                    case "OkButton" :
                        RaiseOkClickedEvent(element);
                        break;
                    case "CancelButton":
                        RaiseCancelClickedEvent(element);
                        break;
                }
        }
    }
}
